﻿package ua.mihaylenko.cardsplugin.tabBar 
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFieldAutoSize;
	public class TabButton extends Sprite
	{
		public var tabText:TextField;
		public var tabBtnUp:Sprite;
		public var tabBtnNormal:Sprite;
		public var tabBtnLast:Sprite;
		private var _isLast:Boolean = false;
		private var _selected:Boolean = false;
		
		public function TabButton() 
		{
			tabText.autoSize = TextFieldAutoSize.LEFT;
			tabText.mouseEnabled = false;
			tabBtnUp.visible = false;
			tabBtnLast.visible = false;
			tabBtnNormal.buttonMode = true;
			tabBtnLast.buttonMode = true;
			tabBtnNormal.addEventListener(MouseEvent.CLICK, mouseClickHandler);
			tabBtnLast.addEventListener(MouseEvent.CLICK, mouseClickHandler);
		}
		
		public function set text(value:String):void
		{
			tabText.text = value;
			var textFormat:TextFormat = new TextFormat();
			textFormat.bold = true;
			tabText.setTextFormat(textFormat);			
			tabText.width = tabText.textWidth + 4;
			tabBtnNormal.width = tabText.width + 24;
			tabBtnUp.width = tabText.width + 16;
			tabBtnLast.width = tabText.width + 16;
		}
		
		public function set isLast(value:Boolean):void
		{
			_isLast = value;
			if (_isLast)
			{
				tabBtnNormal.visible = false;
				tabBtnLast.visible = true;
				//tabBtnUp.height = 21;
				//tabBtnUp.y = 1;
				//tabBtnNormal.width = tabText.width + 16;
			}
		}
		
		public function getWidth():uint
		{
			return tabBtnUp.width - 1;
		}
		
		public function get selected():Boolean
		{
			return _selected;
		}
		
		public function set selected(value:Boolean):void
		{
			_selected = value;
			if (_selected)
			{
				tabBtnNormal.visible = false;
				tabBtnLast.visible = false;
				tabBtnUp.visible = true;
				//tabBtnUp.height = 32;
				tabText.y = 1;
				//tabBtnUp.y = - 8;
				this.dispatchEvent(new TabEvent(TabEvent.TABSELECTED));
			}
			else
			{
				tabBtnUp.visible = false;
				tabBtnNormal.visible = true;
				tabBtnLast.visible = false;
				//tabBtnUp.height = 20;
				tabText.y = 2;
				//tabBtnUp.y = 0;
				this.isLast = _isLast;
			}
		}
		
		private function mouseClickHandler(event:MouseEvent):void
		{
			this.selected = true;
		}
	}	
}